import confetti from 'canvas-confetti'

export function LaunchEffect(
  canvas: WechatMiniprogram.Canvas,
  options: {
    startRect: UniNamespace.NodeInfo
  }
) {
  let verticalPosition = 1 // Default middle
  if (options?.startRect?.top) {
    verticalPosition = options.startRect.top / canvas.height
  }
  createConfetti(canvas, {
    particleCount: 100,
    spread: 80,
    shapes: ['circle', 'circle', 'square'],
    gravity: 0.6,
    startVelocity: 45,
    ticks: 120,
    origin: {
      x: 0.5,
      // y: -0.1 // 屏幕外启动
      y: verticalPosition,
    },
  })
}
export function FallDownEffect(canvas: WechatMiniprogram.Canvas) {
  createConfetti(canvas, {
    particleCount: 100,
    spread: 120,
    gravity: 1,
    ticks: 400,
    shapes: ['square'],
    origin: {
      x: 0.5,
      y: 0, // 屏幕外启动
    },
  })
}

function createConfetti(
  canvas: WechatMiniprogram.Canvas,
  options: confetti.Options = {},
  globalOptions: confetti.GlobalOptions = {}
) {
  const ctor = confetti.create(canvas as any, globalOptions)
  return ctor(options)
}
